//
//  UIImage+Stretch.m
//  HdStore
//
//  Created by zhenqiu on 15/12/7.
//  Copyright (c) 2015年 zhenqiu. All rights reserved.
//

#import "UIImage+Stretch.h"

@implementation UIImage (Stretch)

+ (UIImage *)resizedImage:(NSString *)name{
    
    UIImage * image = [UIImage imageNamed:name];
    
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    
}

- (UIImage *)transformWidth:(CGFloat)width height:(CGFloat)height{
    
    CGFloat destW = width;
    CGFloat destH = height;
    CGFloat sourceW = width;
    CGFloat sourceH = height;
    CGImageRef imageRef = self.CGImage;
    CGContextRef bitmap = CGBitmapContextCreate(NULL, destW, destH, CGImageGetBitsPerComponent(imageRef), 4* destW, CGImageGetColorSpace(imageRef), (kCGBitmapByteOrder32Little| kCGImageAlphaPremultipliedFirst));
    
    CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage * resultImage = [UIImage imageWithCGImage:ref];
    CGContextRelease(bitmap);
    CGImageRelease(ref);
    return resultImage;
    
}


+(UIImage *)getImageFromColor:(UIColor *)color{
    
    CGFloat destW = 5;
    CGFloat destH = 5;
    CGRect rect = CGRectMake(0, 0, destW, destH);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+(UIView*)getGifView:(NSString*)name{
    
    CGRect frame = CGRectMake(0,0,30,30);
    
    NSString * totalName = [NSString stringWithFormat:@"%@.gif",name];
    
    UIImage *image = [UIImage imageNamed:totalName];
    frame.size = CGSizeMake(image.size.width/2, image.size.height/2);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    webView.userInteractionEnabled = NO;//用户不可交互
    
    NSString * html = [NSString stringWithFormat:@"<html><head><body style=\"text-align:center;margin:0;padding:0\"> <img src=\"%@\"><body></head></html>",totalName];
    NSString *path = [[NSBundle mainBundle] resourcePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [webView loadHTMLString:html baseURL:baseURL];
    
    webView.backgroundColor = [UIColor clearColor];
    [webView setOpaque:NO];
    return webView;
}

@end
